package org.brain.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.brain.dbo.DBOperate;
import org.brain.pojos.*;
import org.brain.tool.ToolBean;

@ManagedBean(name="navigatorMenu")
@RequestScoped
public class NavigatorMenu {
	
	TreeNode treeNode = new TreeNode();
	User user;
	public NavigatorMenu(){
		user = ToolBean.getUserSession();
		System.out.println();
	}
	public void iniNavigator(){
		
		
		Treemenu root = new Treemenu();
		root.setNodename("题录");
		root.setChildnode(1);
		root.setUser(user);
		root.setFathernode(0);
		root.setNodeid(0);
		treeNode.setRoot(root);
		treeNode.child.clear();
		addChild(root,treeNode);
		
	}
	public void addChild(Treemenu root,TreeNode node){
		
			List<Treemenu> listMenu = DBOperate.getTreeMenu(user.getUsername(), root.getNodeid());
			node.setSize(listMenu.size());
			
			if(listMenu.size()!=0){
				for(Treemenu u:listMenu)
				{
					System.out.println(u.getNodename());
					TreeNode treeNode = new TreeNode();
					treeNode.setRoot(u);
					treeNode.setSize(0);
					node.child.add(treeNode);
					System.out.println(node.child.size());
				
					if(u.getChildnode()!=0)
					{
						System.out.println("This is a test!"+u.getFathernode());
						addChild(u,treeNode);
					}
				}
			}else
			{
				node.child = new ArrayList();
			}
		
	}
	public TreeNode getTreeNode() {
		iniNavigator();
		return treeNode;
	}
	public void setTreeNode(TreeNode treeNode) {
		this.treeNode = treeNode;
	}


}
